﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;
using System.Xml;


namespace CarInfor.UI
{

    public partial class frmMain : Form
    {
        private const string SYSTEM_TITLE = "宝鸡市鼎信汽车服务有限公司--客户档案管理系统";
        ctlYearCheck ucYearCheck = null;
        ctlInsurance ucInsurance = null;
        ctlYearCheckAlert ucYearCheckAlert = null;
        ctlInsuranceAlert ucInsuranceAlert = null;

        public frmMain()
        {

            InitializeComponent();
            Init();
            this.Text = SYSTEM_TITLE;
            //添加皮肤
            Dictionary<string, string> dic = MyGlobal.GetSkinFiles();
            foreach (string item in dic.Keys)
            {
                ToolStripMenuItem menuitem = new ToolStripMenuItem();
                menuitem.Text = dic[item];
                menuitem.Tag = item;
                this.换肤ToolStripMenuItem.DropDownItems.Add(menuitem);
                menuitem.Click += new EventHandler(menuitem_Click);
            }
        }

        /// <summary>
        /// 换肤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void menuitem_Click(object sender, EventArgs e)
        {
            MyGlobal.shinUI1.LoadSkinFile(Application.StartupPath + "\\Skins\\" + (sender as ToolStripMenuItem).Tag.ToString());
            //重绘
            string title = this.Text;
            this.Text = "";
            this.Text = title;
            //保存皮肤
            string filename = System.Windows.Forms.Application.StartupPath + "\\Config.xml";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            XmlNode skinFile = xmlDoc.SelectSingleNode("Setting/SkinFile");
            skinFile.InnerText = (sender as ToolStripMenuItem).Tag.ToString();
            xmlDoc.Save(filename);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        void Init()
        {
            ucYearCheck = new ctlYearCheck();
            ucYearCheck.Dock = DockStyle.Fill;

            ucInsurance = new ctlInsurance();
            ucInsurance.Dock = DockStyle.Fill;

            ucYearCheckAlert = new ctlYearCheckAlert();
            ucYearCheckAlert.Dock = DockStyle.Fill;

            ucInsuranceAlert = new ctlInsuranceAlert();
            ucInsuranceAlert.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 控制控件显示
        /// </summary>
        /// <param name="myForm"></param>
        private void ShowForm(UserControl myCtl)
        {
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(myCtl);
        }

        private void 年检管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucYearCheck.RefreshUI();
            ShowForm(ucYearCheck);
            this.Text = SYSTEM_TITLE + "--年检管理";
        }

        private void 保险管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucInsurance.RefreshUI();
            ShowForm(ucInsurance);
            this.Text = SYSTEM_TITLE + "--保险管理";
        }

        private void 车辆类型设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCarTypeSet fct = new frmCarTypeSet();
            fct.ShowDialog();
        }

        private void 年检查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucYearCheckAlert.RefreshUI();
            ShowForm(ucYearCheckAlert);
            this.Text = SYSTEM_TITLE + "--年检预警";
        }

        private void 使用性质设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmUsePropertySet fups = new frmUsePropertySet();
            fups.ShowDialog();
        }

        private void 保险险种设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmInsuranceItemSet fiis = new frmInsuranceItemSet();
            fiis.ShowDialog();
        }

        private void 保险预警ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucInsuranceAlert.RefreshUI();
            ShowForm(ucInsuranceAlert);
            this.Text = SYSTEM_TITLE + "--保险预警";
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmPwdUpdate pwd = new frmPwdUpdate();
            pwd.ShowDialog();
        }

        private void 数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmBackup fb = new frmBackup();
            fb.ShowDialog();
        }

        private void 返回首页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(this.pictureBox1);
            this.Text = SYSTEM_TITLE;
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout fa = new frmAbout();
            fa.ShowDialog();
        }

        private void 预警时间设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSetAlert fas = new frmSetAlert();
            fas.ShowDialog();
        }

        private void 短信发送ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(MyGlobal.SmsPath + "GSms.exe");
            }
            catch
            {
                MessageHelper.ShowFail("启动短信模块失败！");
            }
        }

        private void 短信参数设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMsgSettings fs = new frmMsgSettings();
            fs.ShowDialog();
        }
    }
}
